﻿using MyCourseManageProject.Bll;
using MyCourseManageProject.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyCourseManageProject.UI
{
    public partial class FrmAddCourse : Form
    {     
        CoursesBll cbll = new CoursesBll();
        //创建一个课程对象集合,用来保存已经添加成功的课程对象,以便同步展示
        private List<Courses> addCourses = new List<Courses>();
        public FrmAddCourse()
        {
            InitializeComponent();
            //动态填充课程分类下拉框
            this.cbbCategory.DataSource = new CourseCategroysBll().GetCourseCategories();
            this.cbbCategory.DisplayMember = "CategroyName";
            this.cbbCategory.ValueMember = "CategroyId";

            //禁止字段生成列(不需要就不展示)
            this.dgwList.AutoGenerateColumns = false;
        }
   
        /// <summary>
        /// 保存添加的课程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            //1数据校验(检验用户输入信息...请按照项目需求的具体要求做检验  适当的时候请使用正在表达式)
            //2封装数据(将用户输入的数据封装为实体对象)
            Courses cs = new Courses()
            {
                CourseName = this.txtCourseName.Text.Trim(),
                CourseCountent = this.txtCourseContent.Text.Trim(),
                ClassHour =  Convert.ToInt32(this.txtClassHour.Text.Trim()),
                Credit = Convert.ToInt32(this.txtCredit.Text.Trim()),
                CategoryId = Convert.ToInt32(this.cbbCategory.SelectedValue),
                TeacherId = Program.currentTeacher.TeacherId //默认登录用户(实际开发中,依据需求添加下拉框)
            };
            //3调用后台:将当前封装的课程提交该业务逻辑
            string msg;
            bool IsOk = cbll.AddCourses(cs, out msg);
            MessageBox.Show(msg);
            //4同步显示:将添加的数据在列表中同步展示(创建的课程集合对象)
            addCourses.Add(cs);
            this.dgwList.DataSource = null;
            this.dgwList.DataSource = this.addCourses;
            //显示添加的课程总数
            this.lblCount.Text = this.addCourses.Count.ToString();
            //5清空数据
            if (this.ckbAutoClear.Checked)
            {
                foreach (Control item in this.groupBox1.Controls)
                {
                    if (item is TextBox)
                    {
                        item.Text = "";
                    }
                    else if (item is ComboBox)
                    {
                        ((ComboBox)item).SelectedIndex = -1;
                    }
                }
                this.txtCourseName.Focus();//获取焦点
            }

        }
        //关闭窗口
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close(); 
        }
	
    }
}
